home *** CD-ROM | disk | FTP | other *** search
- {------------------------------------------------------------------------------- }
- { Modem Initializer ©1991 Mountainside Software by Tim Damon }
- {This cdev/INIT was written exclusively for my entertainment and education of Mac }
- {programming. I do not offer any warrenty or upgrades to this product as it is meant }
- {purley to be an example of programming a control panel device with an init. If you use }
- {this source code to write your own cdev or INIT, please at least give me some credit }
- {for my hard work. I am willing to answer any questions you may have about this }
- {program and will do my best to answer any questions you may have about writing your }
- {own cdev/INIT. I learned quite a bit from writing this and may be able to help you out. }
- {If you need to contact me or want to send me something like $$$$ or a postcard, send }
- {paper mail to: }
- {Tim Damon }
- {3305 Parade Circle East }
- {Colo. Spgs., CO 80917 }
- {or comments or questions electronically to: }
- {America Online: Screen Name- Yimmit }
- {GEnie: T.Damon }
- {I really hope this helps you programmers out in the area of cdev's and/or INIT's. }
- {Good Luck! }
- {------------------------------------------------------------------------------- }
-
-
- unit ModemCDEV;
-
- interface
- uses
- serial;
-
- const
- ERRDLOG = -4034; {ID # of the error dialog}
-
- G11200 = 1; {Item number of the radio button for 1200 baud rate}
- G12400 = 2; {Item number of the radio button for 2400 baud rate}
- G19600 = 3; {Item number of the radio button for 9600 baud rate}
- G2NO = 4; {Item number of the radio button for no parity}
- G2EVEN = 5; {Item number of the radio button for even parity}
- G2ODD = 6; {Item number of the radio button for odd parity}
- G31 = 7; {Item number of the radio button for 1 stop bit}
- G312 = 8; {Item number of the radio button for 1.5 stop bits}
- G32 = 9; {Item number of the radio button for 2 stop bits}
- G47 = 10; {Item number of the radio button for 7 data bits}
- G48 = 11; {Item number of the radio button for 8 data bits}
- COMMLINE = 12; {Item number of the Command Line}
- SENDNOW = 13; {Item number of the button to do modem initialization now}
- MICON = 14; {Item number of the modem ICON}
- PICON = 15; {Item number of the printer ICON}
-
- BAUD12 = 1; {Value SERC resource holds for 1200 baud rate after decoding}
- BAUD24 = 2; {Value SERC resource holds for 2400 baud rate after decoding}
- BAUD96 = 4; {Value SERC resource holds for 9600 baud rate after decoding}
- PARNONE = 32; {Value SERC resource holds for no parity after decoding}
- PAREVEN = 64; {Value SERC resource holds for even parity after decoding}
- PARODD = 128; {Value SERC resource holds for odd parity after decoding}
- STOP1 = 4; {Value SERC resource holds for 1 stop bit after decoding}
- STOP12 = 8; {Value SERC resource holds for 1.5 stop bits after decoding}
- STOP2 = 16; {Value SERC resource holds for 2 stop bits after decoding}
- DAT7 = 1; {Value SERC resource holds for 7 data bits after decoding}
- DAT8 = 2; {Value SERC resource holds for 8 data bits after decoding}
-
- {cdev message numbers}
- initDev = 0; {Initialize the cdev}
- hitDev = 1; {Handle a hit on an item}
- closeDev = 2; {Close down the cdev}
- nulDev = 3; {Desk accessory run}
- updateDev = 4; {Update any items that the dialog manager won't}
- activDev = 5; {Control Panel becoming active again}
- deActivDev = 6; {Control Panel becoming inactive}
- keyEvtDev = 7; {A keypress happened with the Control Panel active}
- macDev = 8; {Sent if 'mach' resource doesn't contain a specific machine setting}
- undoDev = 9; {Text edit Undo}
- cutDev = 10; {Text edit Cut}
- copyDev = 11; {Text edit Copy}
- pasteDev = 12; {Text edit Paste}
- clearDev = 13; {Text edit Clear}
- cursorDev = 14; {Sent if 'CURS' ID -4064 resource is present - allows your own cursors in the cdev}
- {****For more inofromation on cursorDev refer to TN #215****}
-
- F1 = $7A; {Key code for undo function key on an extended keyboard}
- F2 = $78; {Key code for cut function key on an extended keyboard}
- F3 = $63; {Key code for copy function key on an extended keyboard}
- F4 = $76; {Key code for paste function key on an extended keyboard}
-
- type
-
- ButtonRec = record {A record to hold the state of each group}
- value: integer; {The value of this group}
- item: integer; {The item number of the button that is set in the group}
- end;
-
- GroupRec = record
- Group1: ButtonRec; {This is the radio button for Group 1 (baud rate)}
- Group2: ButtonRec; {This is the radio button for Group 1 (parity)}
- Group3: ButtonRec; {This is the radio button for Group 1 (stop bits)}
- Group4: ButtonRec; {This is the radio button for Group 1 (data bits)}
- CommandText: Str255; {This has to be part of our storage area to maintain a current copy}
- ModemPort: Boolean; {This is the port that will be used in transmissions-->True = modem False = printer}
- end;
-
- GroupPtr = ^GroupRec;
- GroupHnd = ^GroupPtr; {This will be a handle to the GroupRec}
-
- SerConRec = record
- baudRateNPort: Signedbyte; {This will hold the byte containing the baud rate & Port setting when we read it from the resource}
- rest: Signedbyte; {This will hold the encoded byte for parity, stop bits and data bits after reading the resource}
- end;
- SerConPtr = ^SerConRec;
- SerConHnd = ^SerConPtr; {This will be a handle to the serial configuration record}
-
- function main (message, item, numItems, CPanelID: INTEGER; var theEvent: EventRecord; cdevStorage: Longint; CPDialog: DialogPtr): Longint;
-
- implementation
-
- {All of these forward declarations are neccesary in order to keep 'main' as the first code in the code resource}
-
- {==================================================================================}
- procedure ReadResources (var CommandLine: StringHandle; var ConfigHnd: SerConHnd);
- forward;
-
- procedure DecodeConfigResource (ConfigHnd: SerConHnd; var Baud, Par, StopB, DataB, Port: integer);
- forward;
-
- procedure SetButtonON (var ThisButton: ButtonRec; CPDialog: DialogPtr; numItems: integer);
- forward;
-
- procedure SetButtonOFF (var ThisButton: ButtonRec; CPDialog: DialogPtr; numItems: integer);
- forward;
-
- procedure SetUpButtons (var GroupRecord: GroupHnd; Baud, Par, StopB, DataB: integer);
- forward;
-
- procedure SetCommandLine (CPDialog: DialogPtr; CommandLine: StringHandle; numItems: integer);
- forward;
-
- procedure DoClose (var cdevStorage: Longint);
- forward;
-
- procedure DoHit (var cdevStorage: Longint; var GroupRecord: GroupHnd; ItemHit: integer; CPDialog: DialogPtr; numItems: integer);
- forward;
-
- procedure GetGroupValues (GroupRecord: GroupHnd; var Baud, Par, StopB, DataB: integer);
- forward;
-
- procedure EncodeResource (var ConfigHnd: SerConHnd; Baud, Par, StopB, DataB: integer; ModemPort: Boolean);
- forward;
-
- procedure SaveResources (CommandLine: StringHandle; ConfigHnd: SerConHnd);
- forward;
-
- procedure StoreCommandLine (CPDialog: DialogPtr; CommandItem: integer; var GroupRecord: GroupHnd);
- forward;
-
- procedure DoKeyDown (var theEvent: EventRecord; var message: integer; CPDialog: DialogPtr; numItems: integer; var GroupRecord: GroupHnd);
- forward;
-
- procedure DoEdit (message, numItems: INTEGER; CPDialog: DialogPtr; var GroupRecord: GroupHnd);
- forward;
- {=======================================================================================}
-
- {The main entry point MUST be the first code of the cdev}
-
- function main (message, Item, numItems, CPanelID: integer; var theEvent: EventRecord; cdevStorage: Longint; CPDialog: DialogPtr): Longint;
-
- var
- TempH: Handle; {A temporary handle used in GetDItem calls}
- dummyKind: integer; {Dummy integer for use in GetDItem calls}
- r: Rect; {Dummy rect for use in GetDItem calls}
- CommandLine: StringHandle; {A handle to the command line read from the resource}
- ConfigHnd: SerConHnd; {A handle to the serial configuration read from the 'SERC' resource}
- Baud, Par, StopB, DataB, Port: integer; {Used to hold values when working with decoded 'SERC' resource}
- theKey: integer; {Used to hold a key code for a key down event}
- IBeam: CursHandle; {Holds the IBeam cursor resource}
- pt: point; {Used in checking the mouse location to determine what cursor to use}
- WIcon: integer; {The item number of the port icon hilited}
-
- begin
- if (cdevStorage <> 1) and (cdevStorage <> 0) and (cdevStorage <> -1) then {Check for an error}
- begin {No error so proceed with finding what message was sent and take appropriate action}
- case message of
- initDev:
- begin {Initialize the cdev}
- ReadResources(CommandLine, ConfigHnd); {Read the resources into memory}
- cdevStorage := ord4(NewHandle(SIZEOF(GroupRec))); {Set up the storage for the record of radio groups}
- DecodeConfigResource(ConfigHnd, Baud, Par, StopB, DataB, Port); {Decode the configuration resource}
- GroupHnd(cdevStorage)^^.ModemPort := (Port = 16); {Set the modem port flag}
- SetUpButtons(GroupHnd(cdevStorage), Baud, Par, StopB, DataB); {Set up each group's state}
- SetButtonON(GroupHnd(cdevStorage)^^.Group1, CPDialog, numItems); {Set the button for Group 1}
- SetButtonON(GroupHnd(cdevStorage)^^.Group2, CPDialog, numItems); {Set the button for Group 2}
- SetButtonON(GroupHnd(cdevStorage)^^.Group3, CPDialog, numItems); {Set the button for Group 3}
- SetButtonON(GroupHnd(cdevStorage)^^.Group4, CPDialog, numItems); {Set the button for Group 4}
- SetCommandLine(CPDialog, CommandLine, numItems); {Put the command line text in the dialog}
- StoreCommandLine(CPDialog, COMMLINE + numItems, GroupHnd(cdevStorage));
- {Store the command line in our storage area}
-
- ReleaseResource(Handle(CommandLine)); {We are done with this resource, let it go}
- ReleaseResource(Handle(ConfigHnd)); {We are done with this resource, let it go}
- main := cdevStorage; {Set a return value}
- end;
-
- hitDev:
- begin {An item was hit}
- Hlock(Handle(cdevStorage));
- DoHit(cdevStorage, GroupHnd(cdevStorage), Item - numItems, CPDialog, numItems);
- {If an item was hit, let DoHit take care of it}
- HUnlock(Handle(cdevStorage));
- main := cdevStorage; {Set a return value}
- end;
-
- closeDev:
- begin {Prepare for close up}
- DoClose(cdevStorage);
- DisposHandle(Handle(cdevStorage)); {Get rid of the handle so the control panel doesn't go BOOM!}
- cdevStorage := 0; {Set a return value}
- Main := 0;
- end;
-
- deActivDev:
- begin
- Hlock(Handle(cdevStorage));
- ReadResources(CommandLine, ConfigHnd);
- {First read the resources into memory so we have current handles for writing}
- HLock(Handle(CommandLine)); {Lock it down so it doesn't get trashed before we can write out}
- HLock(Handle(ConfigHnd)); {Lock it down so it doesn't get trashed before we can write out}
- GetGroupValues(GroupHnd(cdevStorage), Baud, Par, StopB, DataB); {Get the current settings}
- EncodeResource(ConfigHnd, Baud, Par, StopB, DataB, GroupHnd(cdevStorage)^^.ModemPort);
- {Encode the resource so it can be written}
- HUnLock(Handle(CommandLine)); {Unlock it now so we can change it}
-
- if ord(GroupHnd(cdevStorage)^^.CommandText[Length(GroupHnd(cdevStorage)^^.CommandText)]) <> 13 then
- GroupHnd(cdevStorage)^^.CommandText := concat(GroupHnd(cdevStorage)^^.CommandText, Chr(13));
- {If there isn't a <CR> at the end of the command line, put one on}
-
- SetString(CommandLine, GroupHnd(cdevStorage)^^.CommandText); {Set the new Command line}
- HLock(Handle(CommandLine)); {Lock it down again so it doesn't get trashed before we can write out}
- ChangedResource(Handle(CommandLine)); {Mark resource as changed so it will be written out}
- SaveResources(CommandLine, ConfigHnd); {Write out the resources}
- HUnLock(Handle(CommandLine)); {Let it go now since we don't need it anymore}
- HUnLock(Handle(ConfigHnd)); {Let it go now since we don't need it anymore}
- HUnlock(Handle(cdevStorage));
- Main := cdevStorage; {Set a return value}
- end;
-
- activDev:
- begin
- HLock(Handle(cdevStorage));
- ReadResources(CommandLine, ConfigHnd); {Read the resources into memory}
- DecodeConfigResource(ConfigHnd, Baud, Par, StopB, DataB, Port); {Decode the configuration resource}
- SetUpButtons(GroupHnd(cdevStorage), Baud, Par, StopB, DataB); {Set up each group's state}
- SetButtonON(GroupHnd(cdevStorage)^^.Group1, CPDialog, numItems); {Set the button for Group 1}
- SetButtonON(GroupHnd(cdevStorage)^^.Group2, CPDialog, numItems); {Set the button for Group 2}
- SetButtonON(GroupHnd(cdevStorage)^^.Group3, CPDialog, numItems); {Set the button for Group 3}
- SetButtonON(GroupHnd(cdevStorage)^^.Group4, CPDialog, numItems); {Set the button for Group 4}
- SetCommandLine(CPDialog, CommandLine, numItems); {Put the command line text in the dialog}
- StoreCommandLine(CPDialog, COMMLINE + numItems, GroupHnd(cdevStorage));
- {Store the command line in our storage area}
-
- ReleaseResource(Handle(CommandLine)); {We are done with this resource, let it go}
- ReleaseResource(Handle(ConfigHnd)); {We are done with this resource, let it go}
- HUnlock(Handle(cdevStorage));
- main := cdevStorage; {Set a return value}
- end;
-
- keyEvtDev: {respond to key down}
- begin
- HLock(Handle(cdevStorage));
- DoKeyDown(theEvent, message, CPDialog, numItems, GroupHnd(cdevStorage)); {Handle a key event}
- HUnlock(Handle(cdevStorage));
- end;
-
- cutDev, copyDev, pasteDev, clearDev:
- begin
- HLock(Handle(cdevStorage));
- DoEdit(message, numItems, CPDialog, GroupHnd(cdevStorage)); {Respond to an edit command}
- HUnlock(Handle(cdevStorage));
- end;
-
- cursorDev:
- begin
- HLock(Handle(cdevStorage));
- GetDItem(CPDialog, COMMLINE + numItems, dummykind, TempH, r); {Get the rectangle of the command line}
- GetMouse(pt); {Get the mouse position}
- if PtInRect(pt, r) then {If the mouse is in the text item, set the I-Beam cursor}
- begin
- IBeam := GetCursor(iBeamCursor); {Get the I-Beam cursor}
- SetCursor(IBeam^^); {Set it}
- end
- else
- InitCursor; {Otherwise set the arrow cursor}
- HUnlock(Handle(cdevStorage));
- end;
-
- updateDev:
- begin
- if GroupHnd(cdevStorage)^^.ModemPort then {Find out which port is set}
- WIcon := MICON {Modem Port}
- else
- WIcon := PICON; {Printer Port}
- GetDItem(CPDialog, WIcon + numItems, DummyKind, TempH, r); {Get the rect around the proper icon}
- InvertRect(r); {Hilite the icon}
- end;
-
- nulDev:
- ; {Just a desk accesory run event - basically useless}
- macDev:
- ; {We aren't concerned with macDev since the 'mach' resource contains our machine requirements}
- undoDev:
- ; {Undo is not supported}
-
- end; {CASE message}
-
- main := cdevStorage; {If cdevStorage = -1, 0, or 1 then let the Control Panel put an error message up}
- end;
- end; {main}
-
- {------------------------------------------------------------------------}
- procedure ReadResources (var CommandLine: StringHandle; var ConfigHnd: SerConHnd);
-
- const
- CONFIGID = -4033; {MUST BE BETWEEN -4048 & -4033 FOR NO INTERFERE WITH THE CONTROL PANELS RESOURCES}
- COMMID = -4033; {MUST BE BETWEEN -4048 & -4033 FOR NO INTERFERE WITH THE CONTROL PANELS RESOURCES}
- var
- tempH: Handle; {Temporary storage for reading the SERC Resource}
-
- begin
- CommandLine := GetString(COMMID); {Get the command line resource}
- tempH := GetResource('SERC', CONFIGID); {Get the serial configuration resource}
- ConfigHnd := SerConHnd(TempH); {Typecast generic handle to one the cdev will understand}
- end;
- {------------------------------------------------------------------------}
-
- procedure DecodeConfigResource (ConfigHnd: SerConHnd; var Baud, Par, StopB, DataB, Port: integer);
-
- begin
- with ConfigHnd^^ do
- begin
- Baud := integer(BAND(baudRateNPort, $0F)); {Extract the baud rate as a value the rest of the cdev will recognize}
- Port := integer(BAND(baudRateNPort, $F0)); {Extract the port to be used as a value the rest of the cdev will recognize}
- Par := integer(BAND(rest, $E0)); {Extract the parity value as a value the rest of the cdev will recognize}
- StopB := integer(BAND(rest, $1C)); {Extract the stop bits value as a value the rest of the cdev will recognize}
- DataB := integer(BAND(rest, $03)); {Extract the data bits value as a value the rest of the cdev will recognize}
- end;
- end;
- {------------------------------------------------------------------------}
-
- procedure SetButtonON (var ThisButton: ButtonRec; CPDialog: DialogPtr; numItems: integer);
-
- var
- DummyKind: Integer; {These are dummy variables for the GetDItem calls}
- TempH: Handle;
- r: Rect;
-
- begin
- GetDItem(CPDialog, ThisButton.item + numItems, DummyKind, TempH, r); {Get the handle to the button to be set}
- SetCtlValue(ControlHandle(TempH), 1); {Turn the button on}
- end; {SetButtonON}
- {------------------------------------------------------------------------}
-
- procedure SetButtonOFF (var ThisButton: ButtonRec; CPDialog: DialogPtr; numItems: integer);
-
- var
- DummyKind: Integer; {These are dummy variables for the GetDItem calls}
- TempH: Handle;
- r: Rect;
-
- begin
- GetDItem(CPDialog, ThisButton.item + numItems, DummyKind, TempH, r); {Get the handle to the button to be set}
- SetCtlValue(ControlHandle(TempH), 0); {Turn the button off}
- end; {SetButtonON}
- {------------------------------------------------------------------------}
-
- procedure SetUpButtons (var GroupRecord: GroupHnd; Baud, Par, StopB, DataB: integer);
-
- begin
- with GroupRecord^^ do
- begin
- Group1.Value := Baud;
- case Baud of {Save the appropriate item number}
- BAUD12:
- Group1.Item := G11200; {If the baud rate is 1200, save the item number}
- BAUD24:
- Group1.Item := G12400; {If the baud rate is 2400, save the item number}
- BAUD96:
- Group1.Item := G19600; {If the baud rate is 9600, save the item number}
- end;
-
- Group2.Value := Par;
- case Par of {Save the appropriate item number}
- PARNONE:
- Group2.Item := G2NO; {If there is no parity, save the item number}
- PAREVEN:
- Group2.Item := G2EVEN; {If there is even parity, save the item number}
- PARODD:
- Group2.Item := G2ODD; {If there is odd parity, save the item number}
- end;
-
- Group3.Value := StopB;
- case StopB of {Save the appropriate item number}
- STOP1:
- Group3.Item := G31; {If there is 1 stop bit, save the item number}
- STOP12:
- Group3.Item := G312; {If there are 1.5 stop bits, save the item number}
- STOP2:
- Group3.Item := G32; {If there are 2 stop bits, set save the item number}
- end;
-
- Group4.Value := DataB;
- case DataB of {Save the appropriate item number}
- DAT7:
- Group4.Item := G47; {If there are 7 data bits, save the item number}
- DAT8:
- Group4.Item := G48; {If there are 8 data bits, save the item number}
- end;
- end; {With}
- end; {Set Up Buttons}
- {------------------------------------------------------------------------}
-
- procedure SetCommandLine (CPDialog: DialogPtr; CommandLine: StringHandle; numItems: integer);
-
- var
- dummyKind: integer; {Used in GetDItem calls - just a dummy}
- dummyRect: rect; {Used in GetDItem calls - just a dummy}
- TempHandle: Handle; {A temporary handle to the command line dialog item}
-
- begin
- GetDItem(CPDialog, COMMLINE + numItems, dummyKind, TempHandle, dummyRect);
- {Get a handle to the text item}
-
- SetIText(TempHandle, CommandLine^^); {Set the text item to the command line read from the resource}
-
- SelIText(CPDialog, COMMLINE + numItems, 0, 999);
- {Select the command line so a carat will appear when editing}
- end; {Set Command Line}
- {------------------------------------------------------------------------}
-
- procedure CheckAndSetGroup1 (var Group1: ButtonRec; CPDialog: DialogPtr; ItemHit, numItems: Integer);
-
- begin
- case ItemHit of
- G11200:
- begin
- Group1.Value := BAUD12; {If the item hit was 1200 baud button set new value}
- SetButtonOFF(Group1, CPDialog, numItems); {Turn Off the old button}
- Group1.item := ItemHit; {Set the group state to hold the new item}
- SetButtonON(Group1, CPDialog, numItems); {Turn ON the new button}
- end;
- G12400:
- begin
- Group1.Value := BAUD24; {If the item hit was 2400 baud button set new value}
- SetButtonOFF(Group1, CPDialog, numItems); {Turn Off the old button}
- Group1.item := ItemHit; {Set the group state to hold the new item}
- SetButtonON(Group1, CPDialog, numItems); {Turn ON the new button}
- end;
- G19600:
- begin
- Group1.Value := BAUD96; {If the item hit was 9600 baud button set new value}
- SetButtonOFF(Group1, CPDialog, numItems); {Turn Off the old button}
- Group1.item := ItemHit; {Set the group state to hold the new item}
- SetButtonON(Group1, CPDialog, numItems); {Turn ON the new button}
- end;
- end; {Case for Group1}
- end; {CheckAndSetGroup1}
- {------------------------------------------------------------------------}
-
- procedure CheckAndSetGroup2 (var Group2: ButtonRec; CPDialog: DialogPtr; ItemHit, numItems: Integer);
-
- begin
- case ItemHit of
- G2NO:
- begin
- Group2.Value := PARNONE; {If the item hit was No Parity button set new value}
- SetButtonOFF(Group2, CPDialog, numItems); {Turn Off the old button}
- Group2.item := ItemHit; {Set the group state to hold the new item}
- SetButtonON(Group2, CPDialog, numItems); {Turn ON the new button}
- end;
- G2EVEN:
- begin
- Group2.Value := PAREVEN; {If the item hit was Even Parity button set new value}
- SetButtonOFF(Group2, CPDialog, numItems); {Turn Off the old button}
- Group2.item := ItemHit; {Set the group state to hold the new item}
- SetButtonON(Group2, CPDialog, numItems); {Turn ON the new button}
- end;
- G2ODD:
- begin
- Group2.Value := PARODD; {If the item hit was Odd Parity button set new value}
- SetButtonOFF(Group2, CPDialog, numItems); {Turn Off the old button}
- Group2.item := ItemHit; {Set the group state to hold the new item}
- SetButtonON(Group2, CPDialog, numItems); {Turn ON the new button}
- end;
- end; {Case for Group2}
- end; {CheckAndSetGroup2}
- {------------------------------------------------------------------------}
-
- procedure CheckAndSetGroup3 (var Group3: ButtonRec; CPDialog: DialogPtr; ItemHit, numItems: Integer);
-
- begin
- case ItemHit of
- G31:
- begin
- Group3.Value := STOP1; {If the item hit was 1 stop bit button set new value}
- SetButtonOFF(Group3, CPDialog, numItems); {Turn Off the old button}
- Group3.item := ItemHit; {Set the group state to hold the new item}
- SetButtonON(Group3, CPDialog, numItems); {Turn ON the new button}
- end;
- G312:
- begin
- Group3.Value := STOP12; {If the item hit was 1.5 stop bits button set new value}
- SetButtonOFF(Group3, CPDialog, numItems); {Turn Off the old button}
- Group3.item := ItemHit; {Set the group state to hold the new item}
- SetButtonON(Group3, CPDialog, numItems); {Turn ON the new button}
- end;
- G32:
- begin
- Group3.Value := STOP2; {If the item hit was 2 stop bits button set new value}
- SetButtonOFF(Group3, CPDialog, numItems); {Turn Off the old button}
- Group3.item := ItemHit; {Set the group state to hold the new item}
- SetButtonON(Group3, CPDialog, numItems); {Turn ON the new button}
- end;
- end; {Case for Group3}
- end; {CheckAndSetGroup3}
- {------------------------------------------------------------------------}
-
- procedure CheckAndSetGroup4 (var Group4: ButtonRec; CPDialog: DialogPtr; ItemHit, numItems: Integer);
-
- begin
- case ItemHit of
- G47:
- begin
- Group4.Value := DAT7; {If the item hit was 7 data bits button set new value}
- SetButtonOFF(Group4, CPDialog, numItems); {Turn Off the old button}
- Group4.item := ItemHit; {Set the group state to hold the new item}
- SetButtonON(Group4, CPDialog, numItems); {Turn ON the new button}
- end;
- G48:
- begin
- Group4.Value := DAT8; {If the item hit was 8 data bits button set new value}
- SetButtonOFF(Group4, CPDialog, numItems); {Turn Off the old button}
- Group4.item := ItemHit; {Set the group state to hold the new item}
- SetButtonON(Group4, CPDialog, numItems); {Turn ON the new button}
- end;
- end; {Case for Group4}
- end; {CheckAndSetGroup4}
- {------------------------------------------------------------------------}
-
- procedure DoClose (var cdevStorage: Longint);
-
- var
- CommandLine: StringHandle; {A handle to the command line read from the resource}
- ConfigHnd: SerConHnd; {A handle to the serial configuration read from the 'SERC' resource}
- Baud, Par, StopB, DataB, Port: integer; {Used to hold values when working with decoded 'SERC' resource}
-
- begin
- Hlock(Handle(cdevStorage));
- ReadResources(CommandLine, ConfigHnd);
- {First read the resources into memory so we have current handles for writing}
- HLock(Handle(CommandLine)); {Lock it down so it doesn't get trashed before we can write out}
- HLock(Handle(ConfigHnd)); {Lock it down so it doesn't get trashed before we can write out}
- GetGroupValues(GroupHnd(cdevStorage), Baud, Par, StopB, DataB); {Get the current settings}
- EncodeResource(ConfigHnd, Baud, Par, StopB, DataB, GroupHnd(cdevStorage)^^.ModemPort);
- {Encode the resource so it can be written}
- HUnLock(Handle(CommandLine)); {Unlock it now so we can change it}
-
- if ord(GroupHnd(cdevStorage)^^.CommandText[Length(GroupHnd(cdevStorage)^^.CommandText)]) <> 13 then
- GroupHnd(cdevStorage)^^.CommandText := concat(GroupHnd(cdevStorage)^^.CommandText, Chr(13));
- {If there isn't a <CR> at the end of the command line, put one on}
-
- SetString(CommandLine, GroupHnd(cdevStorage)^^.CommandText); {Set the new Command line}
- HLock(Handle(CommandLine)); {Lock it down again so it doesn't get trashed before we can write out}
- ChangedResource(Handle(CommandLine)); {Mark resource as changed so it will be written out}
- SaveResources(CommandLine, ConfigHnd); {Write out the resources}
- HUnLock(Handle(CommandLine)); {Let it go now since we don't need it anymore}
- HUnLock(Handle(ConfigHnd)); {Let it go now since we don't need it anymore}
- HUnlock(Handle(cdevStorage));
- end;
- {------------------------------------------------------------------------}
-
- procedure PinDialog (CPDialog: DialogPtr; theDialog: DialogPtr);
-
- var
- CPLeft, CPBottom: integer; {The left and bottom coordinates of the Control Panel dialog}
- DTop, DLeft: integer; {The new top and left coordinates of the error dialog - used in pinning it to the control panel}
- pt: Point; {A point to hold the CPDialog's lower left corner}
- SavePort: GrafPtr; {The old port}
-
- begin
- getPort(SavePort); {Get the old port}
- CPLeft := CPDialog^.portrect.Left; {Get the left edge of the control panel window}
- CPBottom := CPDialog^.portrect.Bottom; {Get the bottom edge of the control panel window}
-
- SetPt(pt, CPLeft, CPBottom); {Set a point to the lower left corner of the control panel's window}
- LocalToGlobal(pt); {Change the point to gloabl coordinates}
-
- DLeft := pt.h + 15; {Calculate the new left coordinate}
- DTop := pt.v - 15 - (theDialog^.portrect.bottom - theDialog^.portrect.top);
- {Calculate the new top coordinate}
- MoveWindow(theDialog, DLeft, DTop, True); {pin the error dialog to the bottom-left corner of the control panel window}
- setport(savePort); {Set the port to the old port}
-
- end;
- {------------------------------------------------------------------------}
-
- procedure OutLineDefault (theDialog: DialogPtr);
-
- var
- DummyKind: integer; {A dummy used in GetDItem calls}
- TempH: Handle; {A dummy used in GetDItem calls}
- theRect: Rect; {The rect of the OK button}
- thePen: PenState; {A holding place for the old pen state}
- SavePort: GrafPtr; {The old port}
-
- begin
- getPort(SavePort); {Get the old port}
- setport(theDialog); {Set the port to theDialog}
- GetDItem(theDialog, OK, DummyKind, TempH, theRect); {Get the rectangle of the OK button}
- GetPenState(thePen); {Save the current pen state}
- Pensize(3, 3); {Set the pen to 3 by 3 pixels}
- InsetRect(theRect, -4, -4); {Set the rectangle around the button}
- FrameRoundRect(theRect, 16, 16); {Draw the outline}
- SetPenState(thePen); {Restore the old pen state}
- SetPort(SavePort); {Set the old port}
- end;
- {------------------------------------------------------------------------}
-
-
- function ErrorCheck (Err: OSerr; CPDialog: DialogPtr): Boolean;
-
- var
- tempstr: str255; {Used in unknown error message}
- itemHit: integer; {The item that was hit in the error dialog}
- ErrDialog: DialogPtr; {The error dialog pointer}
-
- begin
- ErrorCheck := False; {Hopefully we won't have an error}
- if Err <> noErr then
- begin
- ErrorCheck := True; {We had an error so let the calling routine know it}
- ErrDialog := GetNewDialog(ERRDLOG, nil, pointer(-1)); {Get the error dialog box}
- PinDialog(CPDialog, ErrDialog); {Pin the dialog to the lower left corner of the control panel dialog}
- case Err of {Set in the error dialog a message saying what error occurred}
- badUnitErr:
- ParamText('Bad ref num', '', '', '');
- dInstErr:
- ParamText('Driver Not Found', '', '', '');
- openErr:
- ParamText('Cannot perform request', '', '', '');
- unitEmptyErr:
- ParamText('Unit Bad ref num', '', '', '');
- dRemovErr:
- ParamText('Attempt to remove an open driver', '', '', '');
- writErr:
- ParamText('Driver cannot respond to write calls', '', '', '');
- NotOpenErr:
- ParamText('Driver not open', '', '', '');
- otherwise
- begin
- numtostring(Err, TempStr);
- TempStr := concat('Unknown error # ', TempStr); {An error not defined above occured - notify user of error number}
- ParamText(TempStr, '', '', '');
- end; {Otherwise}
- end; {CASE}
- SysBeep(3); {Beep like an alert}
- ShowWindow(ErrDialog); {Show the window}
- OutLineDefault(ErrDialog); {Outline the OK button - Item #1}
- repeat
- ModalDialog(nil, itemHit);
- until itemHit = 1;
- DisposDialog(ErrDialog); {Get rid of the error dialog}
- end; {If error}
- end;
- {------------------------------------------------------------------------}
-
- function SetSerialConfig (GroupRecord: GroupHnd): integer;
-
- var
- Baud, Par, StopB, DataB: integer; {Hold the values that will be added together for serial configuration}
- {*****N O T E: These are not the same values that are used by our resource, but the actual toolbox values needed*****}
-
- begin
- with GroupRecord^^ do
- begin
- case Group1.Value of
- BAUD12:
- Baud := Baud1200; {If the baud rate is 1200, set the baud rate value for serial configuration}
- BAUD24:
- Baud := Baud2400; {If the baud rate is 2400, set the baud rate value for serial configuration}
- BAUD96:
- Baud := Baud9600; {If the baud rate is 9600, set the baud rate value for serial configuration}
- end;
-
- case Group2.Value of
- PARNONE:
- Par := NOParity; {If there is no parity, set the parity value for serial configuration}
- PAREVEN:
- Par := EVENParity; {If there is even parity, set the parity value for serial configuration}
- PARODD:
- Par := ODDParity; {If there is odd parity, set the parity value for serial configuration}
- end;
-
- case Group3.Value of
- STOP1:
- StopB := Stop10; {If there is 1 stop bit, set the Stop Bits value for serial configuration}
- STOP12:
- StopB := Stop15; {If there are 1.5 stop bits, set the Stop Bits value for serial configuration}
- STOP2:
- StopB := stop20; {If there are 2 stop bits, set the Stop Bits value for serial configuration}
- end;
-
- case Group4.Value of
- DAT7:
- DataB := Data7; {If there are 7 data bits, set the Data Bits value for serial configuration}
- DAT8:
- DataB := Data8; {If there are 8 data bits, set the Data Bits value for serial configuration}
- end;
- end; {With}
-
- SetSerialConfig := Baud + Par + StopB + DataB; {This is the actual serial configuration value sent to the modem}
- end;
- {------------------------------------------------------------------------}
-
- procedure SendCommand (CPDialog: DialogPtr; var GroupRecord: GroupHnd);
-
- var
- serConfig: integer; {This is the serial port configuration value}
- refnum: integer; {This is the reference number for the modem serial out port}
- Len: Longint; {The Length of the string}
- Error: OSErr; {Will hold the error code if we have an error}
- tempstr: str255; {Will hold the command line}
- TempLong: Longint; {A temporary long integer used to hold the value of cdevStorage}
-
- begin
- if GroupRecord^^.ModemPort then
- Error := OpenDriver('.AOut', refnum) {Open the modem out port}
- else
- Error := OpenDriver('.BOut', refnum); {Open the printer out port}
- if ErrorCheck(Error, CPDialog) then {Check for an error}
- exit(SendCommand); {If an error occurred exit SendCommand}
- serConfig := SetSerialConfig(GroupRecord); {Set up configuration}
- Error := SerReset(refnum, serConfig); {Set the configuration}
- if ErrorCheck(Error, CPDialog) then {Check for an error}
- exit(SendCommand); {If an error occurred exit SendCommand}
- tempStr := GroupRecord^^.CommandText; {Get the command line}
- Len := length(tempStr); {Get the length of the command}
- if ord(tempStr[Len]) <> 13 then
- begin
- tempStr := concat(tempStr, Chr(13)); {If there isn't a <CR> at the end of the command line, put one on}
- Len := Len + 1; {Add one to the length of the string since we added a <CR>}
- end;
-
- Error := FSWrite(refNum, Len, @tempStr[1]); {Write the command line to the modem port}
- if ErrorCheck(Error, CPDialog) then {Check for an error}
- exit(SendCommand); {If an error occurred exit SendCommand}
- Error := CloseDriver(refNum); {Close the port}
- if ErrorCheck(Error, CPDialog) then {Check for an error}
- exit(SendCommand); {If an error occurred exit SendCommand}
- end;
- {------------------------------------------------------------------------}
-
- procedure DoHit (var cdevStorage: Longint; var GroupRecord: GroupHnd; ItemHit: integer; CPDialog: DialogPtr; numItems: integer);
-
- var
- MIconRect, PIconRect: Rect; {The rectangle of the modem and printer icon}
- DummyKind: integer; {A dummy used in GetDItem calls}
- TempH: Handle; {A dummy used in GetDItem calls}
-
- begin
- case ItemHit of
- SENDNOW:
- SendCommand(CPDialog, GroupRecord);
-
- MICON:
- if not GroupRecord^^.ModemPort then
- begin
- GetDItem(CPDialog, MICON + numItems, DummyKind, TempH, MIconRect);
- GetDItem(CPDialog, PICON + numItems, DummyKind, TempH, PIconRect);
- InvertRect(MIconRect); {If the modem icon was hit and it wasn't set already, hilite its icon and...}
- InvertRect(PIconRect); {UnHilite the printer icon}
- GroupRecord^^.ModemPort := True; {Set the new selected port}
- end;
-
- PICON:
- if GroupRecord^^.ModemPort then
- begin
- GetDItem(CPDialog, MICON + numItems, DummyKind, TempH, MIconRect);
- GetDItem(CPDialog, PICON + numItems, DummyKind, TempH, PIconRect);
- InvertRect(PIconRect); {If the printer icon was hit and it wasn't set already, hilite its icon and...}
- InvertRect(MIconRect); {UnHilite the modem icon}
- GroupRecord^^.ModemPort := False; {Set the new selected port}
- end;
- otherwise
- begin
- with GroupRecord^^ do
- begin
- CheckAndSetGroup1(Group1, CPDialog, ItemHit, numItems); {Check if Itemhit in Group 1 if so, set the new button}
- CheckAndSetGroup2(Group2, CPDialog, ItemHit, numItems); {Check if Itemhit in Group 2 if so, set the new button}
- CheckAndSetGroup3(Group3, CPDialog, ItemHit, numItems); {Check if Itemhit in Group 3 if so, set the new button}
- CheckAndSetGroup4(Group4, CPDialog, ItemHit, numItems); {Check if Itemhit in Group 4 if so, set the new button}
- end; {With}
- end; {otherwise}
- end; {Case}
- end; {DoHit}
- {------------------------------------------------------------------------}
-
- procedure GetGroupValues (GroupRecord: GroupHnd; var Baud, Par, StopB, DataB: integer);
-
- begin
- Baud := GroupRecord^^.Group1.Value; {Set the baud value from the button record for Group 1}
- Par := GroupRecord^^.Group2.Value; {Set the parity value from the button record for Group 2}
- StopB := GroupRecord^^.Group3.Value; {Set the stop bits value from the button record for Group 3}
- DataB := GroupRecord^^.Group4.Value; {Set the data bits value from the button record for Group 4}
- end; {Get Group Values}
- {------------------------------------------------------------------------}
-
- procedure EncodeResource (var ConfigHnd: SerConHnd; Baud, Par, StopB, DataB: integer; ModemPort: Boolean);
-
- begin
- ConfigHnd^^.baudRateNPort := SignedByte(Baud); {Set the new baud rate into the configuration}
- if ModemPort then
- ConfigHnd^^.baudRateNPort := ConfigHnd^^.baudRateNPort + 16; {Set the port to be used into the configuration}
- ConfigHnd^^.rest := SignedByte(Par + StopB + DataB); {Set the rest of the stuff in as the rest of the configuration}
- ChangedResource(Handle(ConfigHnd)); {Mark the resource as changed so it will be written}
- end; {Encode Resource}
-
- {------------------------------------------------------------------------}
-
-
- procedure SaveResources (CommandLine: StringHandle; ConfigHnd: SerConHnd);
-
- begin
- WriteResource(Handle(CommandLine)); {Write out the new command line}
- WriteResource(Handle(ConfigHnd)); {Write out the new configuration settings}
- end; {Save Resources}
- {------------------------------------------------------------------------}
-
- procedure StoreCommandLine (CPDialog: DialogPtr; CommandItem: integer; var GroupRecord: GroupHnd);
-
- var
- dummyKind: Integer; {Dummy variable used in GetDItem calls}
- TempH: handle; {Dummy variable used in GetDItem calls}
- r: rect; {Dummy variable used in GetDItem calls}
- TempStr: str255; {A string to hold the command line text}
-
- begin
- GetDItem(CPDialog, CommandItem, dummykind, TempH, r); {Get a handle to the command line}
- GetIText(TempH, TempSTR); {Get the command line from the dialog}
-
- GroupRecord^^.CommandText := TempSTR; {Update our storage to hold the latest command line}
- end;
- {------------------------------------------------------------------------}
-
- procedure DoKeyDown (var theEvent: EventRecord; var message: integer; CPDialog: DialogPtr; numItems: integer; var GroupRecord: GroupHnd);
- var
- tempChar: CHAR; {Used in keyboard messages}
- theKey: integer; {Used in keyboard messages}
- CommandItem: integer; {The item number of the command line plus numItems}
- theButton: Handle; {Handle for GetDItem of SendNow Button}
- dummy: integer; {dummy kind for GetDItem }
- dummyR: rect; {dummy rect for GetDItem }
-
- begin
- tempChar := CHR(BAnd(theEvent.message, charCodeMask)); {Get the character}
- if BAnd(theEvent.modifiers, cmdKey) <> 0 then {Check for a command key equivalent}
- begin { ******* handle as command key ****** }
- message := nulDev;
- theEvent.what := nullEvent; {Erase the event so we don't get it again}
- case tempChar of {If there was a command key then set up the appropriate message}
- 'S', 's':
- begin
- GetDItem(CPDialog, SENDNOW + NumItems, dummy, theButton, dummyR);
- HiliteControl(ControlHandle(theButton), 1); { Flash the Button }
- SendCommand(CPDialog, GroupRecord);
- HiliteControl(ControlHandle(theButton), 0);
- end;
- 'Z', 'z':
- begin
- message := undoDev;{**undoDev is not supported this is only here for someone who wishes to use it**}
- DoEdit(message, numItems, CPDialog, GroupRecord); {Send the new edit message to the edit handler}
- end;
- 'X', 'x':
- begin
- message := cutDev;
- DoEdit(message, numItems, CPDialog, GroupRecord); {Send the new edit message to the edit handler}
- end;
- 'C', 'c':
- begin
- message := copyDev;
- DoEdit(message, numItems, CPDialog, GroupRecord); {Send the new edit message to the edit handler}
- end;
- 'V', 'v':
- begin
- message := pasteDev;
- DoEdit(message, numItems, CPDialog, GroupRecord); {Send the new edit message to the edit handler}
- end;
- end; {case}
- end { ******* handle as command key ****** }
- else
- begin
- theKey := (BitAnd(theEvent.message, KeyCodeMask)) div 256; { decode char }
- case theKey of {Check the key for an edit function key on an extended keyboard}
- F1:
- begin
- message := undoDev;{**undoDev is not supported this is only here for someone who wishes to use it**}
- theEvent.what := nullEvent;
- DoEdit(message, numItems, CPDialog, GroupRecord);
- end;
- F2:
- begin
- message := cutDev; {Set the new message so the edit handler can take care of it}
- theEvent.what := nullEvent; {Wipe out the event so the dialog manager will not handle function keys}
- DoEdit(message, numItems, CPDialog, GroupRecord); {Send the new edit message to the edit handler}
- end;
- F3:
- begin
- message := copyDev; {Set the new message so the edit handler can take care of it}
- theEvent.what := nullEvent; {Wipe out the event so the dialog manager will not handle function keys}
- DoEdit(message, numItems, CPDialog, GroupRecord); {Send the new edit message to the edit handler}
- end;
- F4:
- begin
- message := pasteDev; {Set the new message so the edit handler can take care of it}
- theEvent.what := nullEvent; {Wipe out the event so the dialog manager will not handle function keys}
- DoEdit(message, numItems, CPDialog, GroupRecord); {Send the new edit message to the edit handler}
- end;
- otherwise
- begin
- CommandItem := numItems + COMMLINE; {Create var parameter for DialogSelect}
- if ((tempChar >= 'a') and (tempChar <= 'z')) then {If the character hit is in lowercase ...}
- begin
- tempChar := chr(ord(tempChar) - (ord('a') - ord('A'))); {Force the character to uppercase ...}
- theEvent.message := longint(tempChar); {store the uppercase character in the event so DialogSelect will process it}
- end;
- if (DialogSelect(theEvent, CPDialog, CommandItem) = TRUE) then {Let the dialog manager handle a key event}
- StoreCommandLine(CPDialog, CommandItem, GroupRecord); {Update our storage to hold the new command line}
- theEvent.what := nullEvent; {Wipe out the event so the dialog manager will not process the key since we already did}
- end;
- end; {Case}
- end; {else}
- end;
- {------------------------------------------------------------------------}
-
- procedure DoEdit (message, numItems: INTEGER; CPDialog: DialogPtr; var GroupRecord: GroupHnd);
-
- begin
- case message of
- cutDev:
- DlgCut(CPDialog); {If a cut message was received, do dialog cut}
- copyDev:
- DlgCopy(CPDialog); {If a copy message was received, do dialog copy}
- pasteDev:
- DlgPaste(CPDialog); {If a paste message was received, do dialog paste}
- clearDev:
- DlgDelete(CPDialog); {If a clear message was received, do dialog clear}
- end;
- StoreCommandLine(CPDialog, numItems + COMMLINE, GroupRecord); {Update our storage to hold the new command line}
- end; {DoEditCommand}
-
- end. {ModemCDEV}